package com.ithm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithm.reggie.common.CustomException;
import com.ithm.reggie.dto.DishDto;
import com.ithm.reggie.entity.Dish;
import com.ithm.reggie.entity.DishFlavor;
import com.ithm.reggie.entity.Setmeal;
import com.ithm.reggie.service.DishFlavorService;
import com.ithm.reggie.service.DishService;
import com.ithm.reggie.mapper.DishMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86182
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2024-08-10 12:08:43
*/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存口味
     * @param dishDto
     */
    @Override
    //涉及到多表操作
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品到dish表
        this.save(dishDto);

        //封装id到每个flavor元素
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        //保存口味到flavor表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 修改菜品（根据id查询菜品用于回显数据）
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavors(Long id) {
        Dish d = this.getById(id);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, d.getId());

        List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);

        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(d,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    /**
     * 修改菜品，同时flavor表也做修改
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //保存菜品到dish表
        this.updateById(dishDto);

        //将原来的口味删除
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        //封装id到每个flavor元素
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存口味到flavor表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品，同时删除相应flavor
     */
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids).eq(Dish::getStatus,1);
        int count = this.count(dishLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //删除dish表里面的相应菜品,也就是更新deleted字段
        LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        dishLambdaUpdateWrapper.in(Dish::getId,ids).set(Dish::getIsDeleted,1);
        this.update(dishLambdaUpdateWrapper);

        //删除flavor表里面相应记录,也就是更新deleted字段
        LambdaUpdateWrapper<DishFlavor> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(DishFlavor::getDishId, ids).set(DishFlavor::getIsDeleted,1);
        dishFlavorService.update(lambdaUpdateWrapper);

    }
}




